﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 Simple.SnowFlake.Time
 * 接口名称 TimeExtension
 * 开发人员：11920
 * 创建时间：2023/7/13 13:51:35
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.SnowFlake
{
	public static class DateTimeExtension
	{

		/// <summary>
		/// 默认41位
		/// 年-10位2047
		/// 月-4位 12
		/// 日-4位 31
		/// 时-4位 24
		/// 分钟-5位 60
		/// 秒-5位 60
		/// 毫秒-9位 1000
		/// 每个位占用长度
		/// </summary>
		/// <returns></returns>
		/// 
		//64-1
		private static int DefaultLength=41;
		private static int YearLength =10;
		private static int MonthLength =4;
		private static int DayLength =4;
		private static int HourLength =4;
		private static int MinuteLength =5;
		private static int SecondLength =5;
		private static int MillisecondLength = 9;

		/// <summary>
		/// 各个位需要左移的值
		/// </summary>
		private static int LeftMoveYearLength;
		private static int LeftMoveMonthLength;
		private static int LeftMoveDayLength;
		private static int LeftMoveHourLength;
		private static int LeftMoveMinuteLength;
		private static int LeftMoveSecondLength;

		static DateTimeExtension()
		{
			GetMoveLength();
		}

		private static void GetMoveLength()
		{
			LeftMoveYearLength = DefaultLength - YearLength;
			LeftMoveMonthLength = LeftMoveYearLength - MonthLength;
			LeftMoveDayLength = LeftMoveMonthLength - DayLength;
			LeftMoveHourLength = LeftMoveDayLength - HourLength;
			LeftMoveMinuteLength = LeftMoveHourLength - MinuteLength;
			LeftMoveSecondLength = LeftMoveMinuteLength - SecondLength;
		}

		/// <summary>
		/// 设置年的长度
		/// 默认是10位
		/// 最大值1023
		/// </summary>
		/// <param name="i"></param>
		public static void SetYearLength(int i)
		{
			DefaultLength += i - YearLength;
			YearLength = i;
			GetMoveLength();
		}
		public static long GetTimeTicks(this DateTime time)
		{
			return (long)time.Year << LeftMoveYearLength | (long)time.Month << LeftMoveMonthLength | (long)time.Day << LeftMoveDayLength | (long)time.Hour << LeftMoveHourLength | (long)time.Minute << LeftMoveMinuteLength | (long)time.Second << LeftMoveSecondLength| (long)time.Millisecond;
		}

		public static long GetTimeTicks()
		{
			return (long)DateTime.Now.Year << LeftMoveYearLength | (long)DateTime.Now.Month << LeftMoveMonthLength | (long)DateTime.Now.Day << LeftMoveDayLength | (long)DateTime.Now.Hour << LeftMoveHourLength | (long)DateTime.Now.Minute << LeftMoveMinuteLength | (long)DateTime.Now.Second << LeftMoveSecondLength | (long)DateTime.Now.Millisecond;
		}
	}
}
